<?php

class Silk_Zgb_Block_Adminhtml_Warehouse_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
		public function __construct()
		{
				parent::__construct();
				$this->setId("warehouseGrid");
				$this->setDefaultSort("id");
				$this->setDefaultDir("DESC");
				$this->setSaveParametersInSession(true);
		}

		protected function _prepareCollection()
		{
			$collection = Mage::getModel("zgb/warehouse")->getCollection();

			$websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
			
			$select = $collection->getSelect();
			if(intval($websiteId) !== 0)
			{
				$select->join(array('user' => $collection->getTable('admin/user')), 'user.username=main_table.login_name And user.website_id='.$websiteId);
			}else{
				$select->join(array('user' => $collection->getTable('admin/user')), 'user.username=main_table.login_name',array("website_id"=>'user.website_id'));
			}
			
			$session = Mage::getSingleton('admin/session');
    		$userId = $session->getUser()->getId();
    		$roleId = $session->getUser()->getRoleId();
    		if($roleId == Mage::helper('zgb')->getRoleId('dealer'))
	        {
	        	$model = Mage::getModel('zgb/dealer')->loadByAdminId($userId);
	            $collection->addFieldToFilter('dealer_id',array('eq'=>$model->getId()));
	        }

			$this->setCollection($collection);
			return parent::_prepareCollection();
		}
		protected function _prepareColumns()
		{
			$websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
				$this->addColumn("id", array(
				"header" => Mage::helper("zgb")->__("ID"),
				"align" =>"right",
				"width" => "50px",
			    "type" => "number",
				"index" => "id",
				));
                $this->addColumn("login_name", array(
                "header" => Mage::helper("zgb")->__("配送员"),
                "index" => "login_name",
                ));
                
				$this->addColumn("name", array(
				"header" => Mage::helper("zgb")->__("名字"),
				"index" => "name",
				));
				
				$this->addColumn("contact_name", array(
				"header" => Mage::helper("zgb")->__("姓氏"),
				"index" => "contact_name",
				));
				$this->addColumn("contact_phone", array(
				"header" => Mage::helper("zgb")->__("联系电话"),
				"index" => "contact_phone",
				));
				if(!$websiteId){
					$this->addColumn("website_id", array(
					"header" => Mage::helper("zgb")->__("站点"),
					"index" => "website_id",
					'renderer' => 'Silk_Tms_Block_Renderer_Website',
					));
				}
				$this->addColumn('status', array(
					'header'       =>   Mage::helper('zgb')->__('状态'),
					'index'        =>   'status',
					'type'         =>   'options',
					'options'      =>   array('0' => '禁用', '1' => '启用')
				));
				if(Mage::app()->getRequest()->getParam('shippingId'))
				{
					$this->addColumn('action',
				    array(
				        'header'    => Mage::helper('zgb')->__('分配'),
				        'width'     => '50px',
				        'type'      => 'action',
				        'getter'     => 'getAdminId',
				        'actions'   => array(
				            array(
				                'caption' => Mage::helper('zgb')->__('分配'),
				                'url'     => array(
				                    'base'=>'admin_tms/adminhtml_shipping/shippingbind',
									'params' => array('shippingId' => $this->getRequest()->getParam('shippingId')),
				                ),
								'field' => 'userId'
				            )
				        ),
				        'filter'    => false,
				        'sortable'  => false,
				        'index'     => 'stores',
				    ));
				}
				
			//$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV')); 
			//$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel'));

				return parent::_prepareColumns();
		}

		public function getRowUrl($row)
		{
			if(Mage::app()->getRequest()->getParam('shippingId'))
			{
				
			}
			else
			{
				return $this->getUrl("*/*/edit", array("id" => $row->getId()));
			}
		}


		
		protected function _prepareMassaction()
		{
			$this->setMassactionIdField('id');
			$this->getMassactionBlock()->setFormFieldName('ids');
			$this->getMassactionBlock()->setUseSelectAll(true);
			// $this->getMassactionBlock()->addItem('remove_warehouse', array(
			// 		 'label'=> Mage::helper('zgb')->__('Remove'),
			// 		 'url'  => $this->getUrl('*/adminhtml_warehouse/massRemove'),
			// 		 'confirm' => Mage::helper('zgb')->__('Are you sure?')
			// 	));
			$statuses = array("0"=>"禁用","1"=>"启用");

		    $this->getMassactionBlock()->addItem('status', array(
		         'label'=> Mage::helper('catalog')->__('Change status'),
		         'url'  => $this->getUrl('*/adminhtml_warehouse/massChangeStatus', array('_current'=>true)),
		         'additional' => array(
		                'visibility' => array(
		                     'name' => 'status',
		                     'type' => 'select',
		                     'class' => 'required-entry',
		                     'label' => Mage::helper('zgb')->__('Status'),
		                     'values' => $statuses
		                 )
		         )
		    ));

			return $this;
		}
			

}